package com.lh.springcloud.alibaba.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.lh.springcloud.bean.CommonResult;
import com.lh.springcloud.bean.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class CircleBreakerController {

    //拿到提供者注册中心
    public static final String SERVICE_URL = "http://nacos-payment-provider";

    //轮询算法
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/consumer/fallback/{id}")
    //没做任何配置
    @SentinelResource(value = "fallback")
    public CommonResult<Payment> fallback(@PathVariable Long id) {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id, CommonResult.class, id);

        if (id == 4) {
            throw new IllegalArgumentException("IllegalArgumentException,非法参数异常....");
        } else if (result.getData() == null) {
            throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");
        }

        return result;
    }

    @RequestMapping("/consumer/handlerFallback/{id}")
    //fallback只负责业务异常
    @SentinelResource(value = "handlerFallback", fallback = "handlerFallback")
    public CommonResult<Payment> handlerFallBack(@PathVariable Long id) {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id, CommonResult.class, id);
        System.out.println(result);
        if (id > 3) {
            throw new IllegalArgumentException("IllegalArgumentException,非法参数异常....");
        } else if (result.getData() == null) {
            throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");
        }

        return result;
    }

    //使用fallback兜底方法
    public CommonResult handlerFallback(@PathVariable Long id, Throwable throwable) {
        Payment payment = new Payment(id, "null");
        //输出上方的异常信息
        System.out.println("信息：" + throwable.getMessage());
        return new CommonResult<>(444, "兜底异常handlerFallback,exception内容  " + throwable.getMessage(), payment);
    }

    @RequestMapping("/consumer/blockHandler/{id}")
    //blockHandler负责在sentinel里面配置的降级限流
    @SentinelResource(value = "blockHandler", blockHandler = "blockHandler")
    public CommonResult<Payment> blockHandler(@PathVariable Long id) {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id, CommonResult.class, id);
        System.out.println(result);
        if (id == 4) {
            throw new IllegalArgumentException("IllegalArgumentException,非法参数异常....");
        } else if (result.getData() == null) {
            throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");
        }

        return result;
    }

    //使用blockHandler兜底方法
    public CommonResult blockHandler(@PathVariable Long id, BlockException blockException) {
        Payment payment = new Payment(id, "null");
        return new CommonResult<>(445, "blockHandler-sentinel限流,无此流水: blockException  " + blockException.getMessage(), payment);
    }

    //新增使用fallback兜底方法和使用blockHandler兜底方法
    @RequestMapping("/consumer/fallbackAndBlockHandler/{id}")
    //blockHandler负责在sentinel里面配置的降级限流
    @SentinelResource(value = "fallbackAndBlockHandler", fallback = "handlerFallback", blockHandler = "blockHandler")
    public CommonResult<Payment> fallbackAndBlockHandler(@PathVariable Long id) {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id, CommonResult.class, id);
        System.out.println(result);
        if (id == 4) {
            throw new IllegalArgumentException("IllegalArgumentException,非法参数异常....");
        } else if (result.getData() == null) {
            throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");
        }

        return result;
    }

    //新增exceptionsToIgnore
        @RequestMapping("/consumer/fallbackAndBlockHandlerAndErr/{id}")
    //blockHandler负责在sentinel里面配置的降级限流
    @SentinelResource(value = "fallbackAndBlockHandlerAndErr",
            fallback = "handlerFallback",
            blockHandler = "blockHandler",
            //假如有IllegalArgumentException异常，不再有fallback方法兜底，没有降级效果
            exceptionsToIgnore = {IllegalArgumentException.class})
    public CommonResult<Payment> fallbackAndBlockHandlerAndErr(@PathVariable Long id) {
        CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL + "/paymentSQL/" + id, CommonResult.class, id);
        System.out.println(result);
        if (id == 4) {
            throw new IllegalArgumentException("IllegalArgumentException,非法参数异常....");
        } else if (result.getData() == null) {
            throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");
        }

        return result;
    }

}
